package com.wei.chatroom.config;

import com.wei.chatroom.controller.TestWebSocketController;
import com.wei.chatroom.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * @projectName: java_chatroom
 * @package: com.wei.chatroom.controller.config
 * @className: WebSocketConfig
 * @author: weiJiang
 * @description: 定义完websocket还需要配置使用
 * @date: 2025/9/30 下午4:20
 * @version: 1.0
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private TestWebSocketController testWebSocketController;

    @Autowired
    private WebSocketController webSocketController;

    //通过这个方法把定义的websocket类注册到具体的路径上
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        registry.addHandler(testWebSocketController,"/test");
        registry.addHandler(webSocketController,"/WebSocketmessage")
                .addInterceptors(new HttpSessionHandshakeInterceptor());//通过注册拦截器，可以把会话中添加的session键值对
                                                                        //往我们的webSocket也添加，从而达到获取userId和sessionId的效果
    }
}
